//
//  MBProgressHUD+Extension.m
//  DoulinClient
//
//  Created by Apple on 2018/11/20.
//

#import "MBProgressHUD+Extension.h"
#import "FLAnimatedImageView.h"
#import "FLAnimatedImage.h"

@implementation MBProgressHUD (Extension)

+ (MBProgressHUD *)createHUD:(NSString *)msg {
    return [self showGif];
}

+ (MBProgressHUD *)showGif {
    return [self showGifWithMsg:@"加载中" toView:[UIApplication sharedApplication].delegate.window];
}

+ (MBProgressHUD *)showGifWithMsg:(NSString *)msg toView:(UIView *)view {
    if (view == nil) {
        view = [UIApplication sharedApplication].delegate.window;
    }
    MBProgressHUD * newhud = [[MBProgressHUD alloc] initWithView:view];
    newhud.userInteractionEnabled = YES;
    newhud.bezelView.color = [UIColor clearColor];
    newhud.bezelView.effectView.hidden = NO;
    newhud.backgroundView.color = [UIColor colorWithWhite:0.f alpha:0.f];
    newhud.label.text = msg;
    newhud.label.textColor = [UIColor blackColor];
    newhud.mode = MBProgressHUDModeIndeterminate;
    [view addSubview:newhud];
    
    //如果修改动画图片就在这里修改
    NSURL * imgUrl = [[NSBundle mainBundle] URLForResource:@"app_hudgif" withExtension:@"gif"];
    FLAnimatedImage * animatedImg = [FLAnimatedImage animatedImageWithGIFData:[NSData dataWithContentsOfURL:imgUrl]];
    FLAnimatedImageView * gifImgView = [[FLAnimatedImageView alloc] init];
    gifImgView.frame = CGRectMake(0, 0, 50, 50);
    gifImgView.animatedImage = animatedImg;
    
     newhud.customView = gifImgView;
    [newhud showAnimated:YES];
    return newhud;
}

+ (void)showMsg:(NSString *)msg{
    MBProgressHUD * hud = [self createHUD1:nil];
    hud.detailsLabel.font = [UIFont boldSystemFontOfSize:16];
    hud.label.textColor = [UIColor whiteColor];
    hud.mode = MBProgressHUDModeText;
    hud.label.text = msg;
    [hud hideAnimated:YES afterDelay:1.4];
}
+ (void)showMsg:(NSString *)msg time:(CGFloat)time {
    MBProgressHUD * hud = [self createHUD1:nil];
    hud.detailsLabel.font = [UIFont boldSystemFontOfSize:16];
    hud.label.textColor = [UIColor whiteColor];
    hud.mode = MBProgressHUDModeText;
    hud.label.text = msg;
    [hud hideAnimated:YES afterDelay:time];
}
+ (MBProgressHUD *)createHUD1:(NSString *)msg {
    UIWindow * window = [UIApplication sharedApplication].keyWindow;
    if (!window) return nil;
    
    MBProgressHUD * hud = [[MBProgressHUD alloc] initWithView:window];
    hud.backgroundView.color = [[UIColor blackColor] colorWithAlphaComponent:0.2];
    hud.detailsLabel.font = [UIFont boldSystemFontOfSize:11];
    hud.mode = MBProgressHUDModeIndeterminate;
    hud.contentColor = [UIColor whiteColor];
    hud.label.text = msg ? msg : @"加载中";
    hud.label.textColor = RGB(250, 250, 250, 1);
    hud.removeFromSuperViewOnHide = YES;
    hud.bezelView.color = Black_Color;
    hud.bezelView.alpha=0.7;
    hud.square = [msg isEqualToString:@"加载中"];
    hud.margin = 8;
    [window addSubview:hud];
    
    [hud showAnimated:YES];
    //[hud addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:hud action:@selector(hide:)]];
    
    return hud;
}
+ (void)hide:(MBProgressHUD *)hud {
    [hud hideAnimated:YES];
}

+ (MBProgressHUD *)hudAddProgress:(UIView *)addView
{
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:addView animated:YES];
    return hud;
}

+ (void)deleteProgress:(MBProgressHUD *)hud
{
    [hud hideAnimated:YES];
}

//#pragma mark - ------------------ <#note#> ------------------
//+ (MBProgressHUD *)showMessage:(NSString *)message {
//    return [self showMessage:message toView:nil];
//}
//+ (MBProgressHUD *)showMessage:(NSString *)message toView:(UIView *)view {
//    if (view == nil) {
//        view = [[UIApplication sharedApplication].windows lastObject];
//    }
//
//    // 快速显示一个提示信息
//    MBProgressHUD * hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
//    hud.label.text = message;
//
//    // 隐藏时候从父控件中移除
//    hud.removeFromSuperViewOnHide = YES;
//
//    // YES代表需要蒙版效果
//    hud.backgroundView.color = [UIColor colorWithWhite:0.f alpha:0.1f];
//
//    return hud;
//}
//
//+ (void)showSuccess:(NSString *)success {
//    [self showSuccess:success toView:nil];
//}
//+ (void)showError:(NSString *)error {
//    [self showError:error toView:nil];
//}
//+ (void)showError:(NSString *)error toView:(UIView *)view {
//    [self show:error icon:@"error.png" view:view];
//}
//+ (void)showSuccess:(NSString *)success toView:(UIView *)view {
//    [self show:success icon:@"success.png" view:view];
//}
//+ (void)show:(NSString *)text icon:(NSString *)icon view:(UIView *)view {
//    if (view == nil) {
//        view = [[UIApplication sharedApplication].windows lastObject];
//    }
//
//    // 快速显示一个提示信息
//    MBProgressHUD * hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
//    hud.label.text = text;
//
//    // 设置图片
//    hud.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[NSString stringWithFormat:@"MBProgressHUD.bundle/%@", icon]]];
//
//    // 再设置模式
//    hud.mode = MBProgressHUDModeCustomView;
//
//    // 隐藏时候从父控件中移除
//    hud.removeFromSuperViewOnHide = YES;
//
//    // 0.9秒之后再消失
//    [hud hideAnimated:YES afterDelay:0.9];
//}
//
//+ (void)hideHUD {
//    UIView * view = [[UIApplication sharedApplication].windows lastObject];
//    [self hideHUDForView:view];
//}
//+ (void)hideHUDForView:(UIView *)view {
//    [self hideHUDForView:view animated:YES];
//}

@end
